/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.ui.view;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.yc.video.ResourceTable;
import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.controller.IGestureComponent;
import com.yc.video.tool.PlayerUtils;

/**
 * 手势控制
 */

/**
 * <pre>
 *     @author yangchong
 *     blog  : https://github.com/yangchong211
 *     time  : 2017/11/9
 *     desc  : 手势控制
 *     revise: 用于滑动改变亮度和音量的功能
 * </pre>
 */
public class CustomGestureView extends StackLayout implements IGestureComponent {
    private ControlWrapper mControlWrapper;
    private DirectionalLayout mLlCenterContainer;
    private Image mIvIcon;
    private Text mTvPercent;
    private ProgressBar mProPercent;

    public CustomGestureView(Context context) {
        super(context);
        init(context);
    }

    public CustomGestureView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    public CustomGestureView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        setVisibility(HIDE);
        Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_custom_video_player_gesture, this, true);
        initFindViewById(view);
        initListener();
    }

    private void initFindViewById(Component view) {
        mLlCenterContainer = (DirectionalLayout) view.findComponentById(ResourceTable.Id_ll_center_container);
        mIvIcon = (Image) view.findComponentById(ResourceTable.Id_iv_icon);
        mTvPercent = (Text) view.findComponentById(ResourceTable.Id_tv_percent);
        mProPercent = (ProgressBar) view.findComponentById(ResourceTable.Id_pro_percent);
    }

    private void initListener() {
    }

    @Override
    public void attach(ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animator anim) {
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
    }

    /**
     * 开始滑动
     */
    @Override
    public void onStartSlide() {
        mControlWrapper.hide();
        mLlCenterContainer.setVisibility(VISIBLE);
        mLlCenterContainer.setAlpha(1f);
    }

    /**
     * 结束滑动
     * 这个是指，手指抬起或者意外结束事件的时候，调用这个方法
     */
    @Override
    public void onStopSlide() {
        mLlCenterContainer.createAnimatorProperty()
            .alpha(0f)
            .alphaFrom(1f)
            .setDuration(300)
            .setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                }

                @Override
                public void onStop(Animator animator) {
                }

                @Override
                public void onCancel(Animator animator) {
                }

                @Override
                public void onEnd(Animator animator) {
                    mLlCenterContainer.setVisibility(HIDE);
                }

                @Override
                public void onPause(Animator animator) {
                }

                @Override
                public void onResume(Animator animator) {
                }
            })
            .start();
    }

    /**
     * 滑动调整进度
     *
     * @param slidePosition 滑动进度
     * @param currentPosition 当前播放进度
     * @param duration 视频总长度
     */
    @Override
    public void onPositionChange(int slidePosition, int currentPosition, int duration) {
        mProPercent.setVisibility(HIDE);
        if (slidePosition > currentPosition) {
            mIvIcon.setPixelMap(ResourceTable.Media_ic_player_fast_forward);
        } else {
            mIvIcon.setPixelMap(ResourceTable.Media_ic_player_fast_rewind);
        }
        mTvPercent.setText(String.format("%s/%s", PlayerUtils.formatTime(slidePosition), PlayerUtils.formatTime(duration)));
    }

    /**
     * 滑动调整亮度
     *
     * @param percent 亮度百分比
     */
    @Override
    public void onBrightnessChange(int percent) {
        mProPercent.setVisibility(VISIBLE);
        mIvIcon.setPixelMap(ResourceTable.Media_ic_palyer_brightness);
        mTvPercent.setText(percent + "%");
        mProPercent.setProgressValue(percent);
    }

    /**
     * 滑动调整音量
     *
     * @param percent 音量百分比
     */
    @Override
    public void onVolumeChange(int percent) {
        mProPercent.setVisibility(VISIBLE);
        if (percent <= 0) {
            mIvIcon.setPixelMap(ResourceTable.Media_ic_player_volume_off);
        } else {
            mIvIcon.setPixelMap(ResourceTable.Media_ic_player_volume_up);
        }
        mTvPercent.setText(percent + "%");
        mProPercent.setProgressValue(percent);
    }

    @Override
    public void onPlayStateChanged(int playState) {
        if (playState == ConstantKeys.CurrentState.STATE_IDLE
            || playState == ConstantKeys.CurrentState.STATE_START_ABORT
            || playState == ConstantKeys.CurrentState.STATE_PREPARING
            || playState == ConstantKeys.CurrentState.STATE_PREPARED
            || playState == ConstantKeys.CurrentState.STATE_ERROR
            || playState == ConstantKeys.CurrentState.STATE_BUFFERING_PLAYING
            || playState == ConstantKeys.CurrentState.STATE_ONCE_LIVE) {
            setVisibility(HIDE);
        } else {
            setVisibility(VISIBLE);
        }
    }

    @Override
    public void setProgress(int duration, int position) {
    }

    @Override
    public void onLockStateChanged(boolean isLock) {
    }
}
